package org.mistfx.decoration.demo;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.mistfx.decoration.Decoration;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * Created on 2015/6/11.
 *
 * @author Misty
 */
public class DemoApp extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Controller controller = new Controller();
        Decoration decoration = new Decoration(controller);
//        decoration.setPadding(new Insets(35, 10, 10, 10));
        controller.setDecoration(decoration);

        Scene scene = new Scene(decoration, 400, 300, Color.TRANSPARENT);

        primaryStage.maximizedProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("stage maximized " + newValue);
        });
        primaryStage.setTitle("DemoApp");
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setScene(scene);
        primaryStage.show();

        primaryStage.setOnCloseRequest(event ->
                        System.out.println("close request")
        );
    }

    static class Controller extends AnchorPane implements Initializable {
        private ObjectProperty<Decoration> decoration = new SimpleObjectProperty<>();

        @FXML private ToggleButton shadowVisible;
        @FXML private TextField title;

        @FXML private ColorPicker backgroundFill;
        @FXML private Slider backgroundOpacity;
        @FXML private Label backgroundOpacityText;
        @FXML private CheckBox utilityMode;

        public Controller() {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("test.fxml"));
            loader.setRoot(this);
            loader.setController(this);
            try {
                loader.load();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void initialize(URL location, ResourceBundle resources) {
            decoration.addListener((observable, oldValue, control) -> {
                if (control != null) {
                    this.shadowVisible.selectedProperty().bindBidirectional(control.shadowVisibleProperty());
                    this.title.textProperty().bindBidirectional(control.titleProperty());
                    this.backgroundFill.setValue((Color) control.getBackgroundFill());
                    this.backgroundFill.setOnAction(event -> control.setBackgroundFill(backgroundFill.getValue()));

                    backgroundOpacity.valueProperty().bindBidirectional(control.backgroundOpacityProperty());
                    backgroundOpacityText.textProperty().bind(Bindings.convert(backgroundOpacity.valueProperty()));

                    this.utilityMode.selectedProperty().bindBidirectional(control.utilityModeProperty());
                }
            });
        }

        public void setDecoration(Decoration decoration) {
            this.decoration.set(decoration);
        }
    }
}
